using System;
using System.Threading.Tasks;
using SCore.Util;
using UnityEngine;
using Util.Pool;

namespace Function.SkillEditor.Runtime
{
    public class EffectRender
    {
        public static async void PlayEffect(Transform bindTran, GameObject asset, Vector3 pos,
            Vector3 rotate, Vector3 scale, Action<GameObject> callback = null)
        {
            var addressable = asset.name + ".prefab";
            if (Pool.recycleBinForGameObjectName(addressable) == null)
            {
                var recycleBin = new TrashManRecycleBin()
                {
                    imposeHardLimit = true, //存在上线
                    hardLimit = 10, //上限
                    cullExcessPrefabs = true, //需要剔除
                    instancesToMaintainInPool = 2, //保留数量
                    cullInterval = 10, //剔除间隔
                    persistBetweenScenes = false, //持久化
                    addressable = addressable,
                    binLoadType = BinLoadType.Sync,
                };
                Pool.manageRecycleBin(recycleBin);
            }

            var effectObj = await Pool.spawn(addressable);
            if (effectObj == null)
            {
                return;
            }

            if (bindTran != null)
            {
                effectObj.transform.SetParent(bindTran);
            }

            effectObj.transform.SetPositionAndRotation(pos, Quaternion.Euler(rotate));
            effectObj.transform.localScale = scale;
            callback?.Invoke(effectObj);
        }

        public static void StopEffect(GameObject effectGo)
        {
            Pool.despawn(effectGo);
        }
    }
}